import React, {Component} from 'react';
import {base, high} from 'nc-lightapp-front';
import {formatDate} from "../../../../../../../hrpub/common/utils/utils";

const {Refer} = high;
const {NCRow, NCCol, NCCheckbox, NCTextArea, NCInput, NCDatePicker, NCSwitch} = base;

function checkedHandle(item, value) {
    if (item.key !== 'endflag' && item.key !== 'enddate') {
        this.setState({
            [item.key + '_flag']: value
        })
    } else {
        const {endflag_flag, enddate_flag} = this.state;
        if (!endflag_flag && !enddate_flag) {
            this.setState({
                endflag_flag: true,
                endflag: true,
                enddate_flag: true,
                enddate: formatDate(new Date())
            })
        } else {
            this.setState({
                [item.key + '_flag']: value
            })
        }
    }
}

export default function getCompItem(item) {
    let compItem;
    switch (item.type) {
        case 'input':
            compItem = <NCInput
                value={this.state[item.key]}
                onChange={(value) => this.setState({
                    [item.key]: value
                })}
                disabled={!this.state[item.key + '_flag']}
            />;
            break;
        case "datePickerNoTimeZone":
            compItem = (
                <NCDatePicker
                    format={item.format || 'YYYY-MM-DD'}
                    onChange={(value) => this.setState({
                        [item.key]: value
                    })}
                    value={this.state[item.key]}
                    disabled={!this.state[item.key + '_flag']}
                />
            );
            break;
        case 'refer':
            compItem = <Refer
                {...item.refConfig}
                refName={item.name}
                placeholder={item.name}
                value={this.state[item.key]}
                onChange={(value) => this.setState({
                    [item.key]: value
                })}
                disabled={!this.state[item.key + '_flag']}
            />;
            break;
        case 'checkbox':
            compItem = <NCCheckbox
                onChange={(value) => this.setState({
                    [item.key]: value
                })}
                checked={this.state[item.key]}
                disabled={!this.state[item.key + '_flag']}
            />;
            break;
        case 'switch':
            compItem = <NCSwitch
                onChange={(value) => this.setState({
                    [item.key]: value
                })}
                checked={this.state[item.key]}
                disabled={!this.state[item.key + '_flag']}
            />;
            break;
        case 'textarea':
            compItem = <NCTextArea
                value={this.state[item.key]}
                onChange={(value) => this.setState({
                    [item.key]: value
                })}
                disabled={!this.state[item.key + '_flag']}
            />;
            break;
        default:
            break;
    }
    return <NCRow key={item.key}>
        <NCCol md={1}>
            <NCCheckbox
                onChange={(value) => checkedHandle.call(this, item, value)}
                checked={this.state[item.key + '_flag']}
            />
        </NCCol>
        <NCCol md={2}>
            <span>{item.name}</span>
        </NCCol>
        <NCCol md={9}>
            {compItem}
        </NCCol>
    </NCRow>;
}
